package org.unique.weibooauthtest.activity;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.http.HttpParameters;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ShowOauthResult extends Activity implements OnClickListener{
	Config config;
	OAuth oAuth;
	static int SELECT_PICTURE=0;
	String picPath =null;
	TextView textView1;
	String userId;
	String userKey ;
	String userSecret;
	String status = "";
	EditText editText1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showresult_view);
		
		config = Config.getInstance(); 
		oAuth = OAuth.getInstance();
		
		textView1= (TextView) findViewById(R.id.textView1);
		editText1 = (EditText)findViewById(R.id.editText1);
		Button button1 = (Button)findViewById(R.id.button1);
		Button button2 = (Button)findViewById(R.id.button2);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		
		Uri uri = getIntent().getData();
		String verifier = uri
				.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
		Log.i("verifier", verifier);
		oAuth.getAccessToken(verifier);
		userId = oAuth.getUsetId();
		userKey = oAuth.getOAuthToken();
		userSecret = oAuth.getOAuthTokenSecret();

		textView1.setText("suerId:" + userId + "\nuserKey:" + userKey
				+ "\nuserSecret:" + userSecret);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
             intent.setType("image/*"); 
             startActivityForResult(Intent.createChooser(intent, "选择图片"), 
                     SELECT_PICTURE); 
			break;

		case R.id.button2:
			status = editText1.getText().toString();
			oAuth.share2weibo(userKey, userSecret, status,picPath);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		ContentResolver contentResolver = getContentResolver();
		if (requestCode == SELECT_PICTURE) {
			try {
				Uri uri = data.getData();
				String[] proj = {MediaStore.Images.Media.DATA};
				Cursor cursor = managedQuery(uri, proj, null, null, null);
	            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
	            cursor.moveToFirst();
	            picPath = cursor.getString(column_index);
	            textView1.setText(picPath);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
}
